# -*- coding:utf-8 -*-
# 字典的常用操作
# 1、增加数据
# 写法：字典序列[key] = 值
# 注意：如果key存在则修改这个key对应的值；如果key不存在则新增此键值对。
# dict1 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# dict1["姓名"] = "李四"   # key存在，改变值
# print(dict1)
# dict1["爱好"] = "打篮球" # key不存在，新增键值对
# print(dict1)


# 2、删除数据
# 删除字典或删除字典中指定键值对
# del()/del
# dict2 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# del dict2["姓名"]
# print(dict2)
#
# del dict2
# print(dict2)    # 删除整个字典会报错  # NameError: name 'dict1' is not defined

# 清空字典
# clear()
# dict3 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# dict3.clear()
# print(dict3)
# set1 = {1, 2, 3, 4, 5}
# set1.clear()
# print(set1)


# 3、查找数据
# key根据键查找值
# dict3 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# print(dict3["姓名"])
# print(dict3['id'])  # 报错

# get()
# 字典序列.get(key, 默认值)
# dict3 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# print(dict3.get("姓名"))
# print(dict3.get('id', 110))  # 110
# print(dict3.get('id'))  # None


# 键值对  key value
# 总结字典的增删改查
# dict1 = {"姓名": "张三", "年龄": 18, "性别": "男"}
# # 增加数据
# dict1["爱好"] = "篮球"
# print(dict1)
# # 修改数据
# dict1["姓名"] = "小宋"
# print(dict1)
# # 删除数据
# del dict1["性别"]
# print(dict1)
# # 查找数据
# print(dict1["姓名"])
# print(dict1)
